package com.niiwoo.civet.trade.service.local.newRefund.notify.dubbo;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.niiwoo.civet.trade.dao.entity.Project;
import com.niiwoo.civet.trade.dao.entity.RefundNotifyRecord;
import com.niiwoo.civet.trade.service.local.newRefund.common.RefundCommonService;
import com.niiwoo.civet.trade.service.local.newRefund.notify.RefundNotifyHandle;
import com.niiwoo.civet.trade.service.local.newRefund.notify.dto.RefundNotifyDTO;
import com.niiwoo.civet.trade.service.local.newRefund.notify.dto.RefundNotifyHandleResult;
import com.niiwoo.civet.trade.service.local.newRefund.notify.enums.RefundNotifyEnum;
import com.niiwoo.tripod.tiancheng.component.TianchengDataFlowService;
import com.niiwoo.tripod.tiancheng.request.df.ProjectStatusChangeDataFlowRequest;
import com.niiwoo.tripod.tiancheng.response.common.TianchengUapResponse;

import lombok.extern.slf4j.Slf4j;

/**
 * 天称-标的状态变更回流
 * 
 * 触发条件：标的逾期、标的结清、标的逾期到正常还款
 * 
 * @author: kevin.jia
 * @version 1.0.0
 */
@Slf4j
@Service
public class TianChengProjectStatusChangeNotify implements RefundNotifyHandle {

	@Autowired
	private RefundCommonService refundCommonService;

	@Autowired
	private TianchengDataFlowService tianchengDataFlowService;

	@Override
	public RefundNotifyHandleResult handle(RefundNotifyDTO refundNotifyDTO) {
		RefundNotifyHandleResult refundNotifyHandleResult = null;
        try {
        	ProjectStatusChangeDataFlowRequest projectStatusChangeDataFlowRequest = (ProjectStatusChangeDataFlowRequest) bulidParam(refundNotifyDTO);
        	
        	log.info("天称-标的状态变更回流，ProjectId：{}， 请求参数：{}", refundNotifyDTO.getProject().getProjectId(), JSONObject.toJSONString(projectStatusChangeDataFlowRequest));
            CompletableFuture<TianchengUapResponse> completableFuture = tianchengDataFlowService.projectStatusChange(projectStatusChangeDataFlowRequest);
            TianchengUapResponse tianchengUapResponse = completableFuture.get(3, TimeUnit.SECONDS);
            log.info("天称-标的状态变更回流，ProjectId：{}， 返回结果：{}", refundNotifyDTO.getProject().getProjectId(), JSONObject.toJSONString(tianchengUapResponse));
            refundNotifyHandleResult = new RefundNotifyHandleResult();
        } catch (Exception e) {
        	refundNotifyHandleResult = new RefundNotifyHandleResult(e.getMessage());
            log.error("天称-标的状态变更回流异常，异常原因：{}， 请求参数：{}", e.getMessage(), JSONObject.toJSONString(refundNotifyDTO.getRefundNotifyRecord()), e);
        }
        return refundNotifyHandleResult;
	}
	
	@Override
	public Object bulidParam(RefundNotifyDTO refundNotifyDTO) {
		Project project = refundNotifyDTO.getProject();
    	RefundNotifyRecord refundNotifyRecord = refundNotifyDTO.getRefundNotifyRecord();

		ProjectStatusChangeDataFlowRequest projectStatusChangeDataFlowRequest = new ProjectStatusChangeDataFlowRequest();
		projectStatusChangeDataFlowRequest.setProjectId(String.valueOf(project.getProjectId()));
		projectStatusChangeDataFlowRequest.setUpdateTime(String.valueOf(System.currentTimeMillis() * 1000));
		projectStatusChangeDataFlowRequest.setFromType(refundCommonService.transformProjectStatus4TianCheng(refundNotifyRecord.getFromSubStatus()));
		projectStatusChangeDataFlowRequest.setType(refundCommonService.transformProjectStatus4TianCheng(project.getSubStatus()));

		return projectStatusChangeDataFlowRequest;
	}
	
	@Override
	public RefundNotifyEnum getType() {
		return RefundNotifyEnum.TIANCHENG_PROJECT_STATUS;
	}	
}